package com.weibo.demos.web.Controller;


import com.weibo.demos.web.Mapper.CommentMapper;
import com.weibo.demos.web.entity.CommentDTO;
import com.weibo.demos.web.entity.UserVO;
import com.weibo.demos.web.entity.WeiboVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
@Slf4j
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("/insert")
    public int insertComment(@RequestBody CommentDTO commentDTO, HttpServletRequest request){
        //获取当前用户信息
        UserVO user = (UserVO) request.getSession().getAttribute("user");
        if (user==null){
            return 2;
        }
        //获取当前用户id
        Integer userId = user.getId();
        commentDTO.setUserId(userId);
        //调用Mapper接口保存当前用户评论
        commentMapper.insert(commentDTO);
        return 1;
    }

    @GetMapping("/selectByWeiboId")
    public List<WeiboVO> selectByWeiboId(@RequestParam(value ="id",required = false) Integer weiboId){
        log.info("根据微博id{}查询微博评论",weiboId);
        return commentMapper.selectByWeiboId(weiboId);
    }

}
